package com.chao.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.codec.support.DefaultServerCodecConfigurer;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author : Rookie
 * @description :
 * @date : 2022/6/3
 */
@Configuration
public class CorsConfig {

    /**
     * @description 跨域配置
     * @author fu wenchao
     * @date 2022/1/3 13:21
     * @return org.springframework.web.cors.reactive.CorsWebFilter
     */
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //配置跨域
        CorsConfiguration config = new CorsConfiguration();
        //任意请求头
        config.addAllowedHeader("*");
        //任意方式
        config.addAllowedMethod("*");
        //任意请求来源
        config.addAllowedOrigin("*");
        //允许携带cookie跨域
        config.setAllowCredentials(true);
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }

    @Bean
    public ServerCodecConfigurer serverCodecConfigurer() {
        return new DefaultServerCodecConfigurer();
    }
}
